package xyf.p04.tree;

import commons.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: P0144_PreOrder
 * Description:
 * Author: xyf
 * Date: 9/4/22 9:10 AM
 * Version: 1.0
 **/
public class P0144_PreOrder
{
    public List<Integer> preorderTraversal(TreeNode root)
    {
        List<Integer> result = new ArrayList<>();
        if (root == null)
        {
            return result;
        }
        pre(result, root);
        return result;
    }

    private void pre(List<Integer> result, TreeNode root)
    {
        result.add(root.val);
        if (root.left != null)
        {
            pre(result, root.left);
        }
        if (root.right != null)
        {
            pre(result, root.right);
        }
    }
}
